package com.example.isoftservice.service.impl;

import com.example.isoftservice.mapper.AddressMapper;
import com.example.isoftservice.pojos.Address;
import com.example.isoftservice.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;
    @Override
    @Cacheable(cacheNames ="address",key="#pageNo+pageSize")
    public Map<String, Object> searchAddressList(Integer pageNo,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNo,pageSize);
        List<Map<String,Object>> resultList = addressMapper.selectAddressList();
        PageInfo<Map<String,Object>> info = new PageInfo<>(resultList);
        map.put("total",info.getTotal());
        map.put("rows",info.getList());
        return map;
    }

    @Override
    @Cacheable(cacheNames ="address",key="#addressId")
    public Address searchAddressById(Integer addressId) {
        return addressMapper.selectAddressById(addressId);
    }

    @Override
    @CacheEvict(cacheNames = "address" ,allEntries = true)
    public Boolean addAddress(Address address) {
        return addressMapper.insertAddress(address) == 1 ? true : false;
    }

    @Override
    @CacheEvict(cacheNames = "address" ,allEntries = true)
    public Boolean removeAddressByIds(Integer[] ids) {
        return addressMapper.deleteAddressByIds(ids) >= 1 ? true : false;
    }

    @Override
    @CacheEvict(cacheNames = "address" ,allEntries = true)
    public Boolean modifyAddressById(Address address) {
        return addressMapper.updateAddressById(address) == 1 ? true : false;
    }
}
